package com.central.apps.handler.contract;

import com.central.app.entity.TBFile;
import com.central.app.service.CheckConNodeService;
import com.central.app.service.HandlerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2022/9/23.
 */
@Slf4j
@Service
public class FileSynUtilServiceImpl implements  FileSynUtilService{
    @Resource
    private CheckConNodeService checkConNodeService;
    @Autowired
    private HandlerService handlerService;
    public void insertFiles(String appname,Long id,String concode ){
        String accountsql = "select * from gq_conaccount where  concode = '"+concode+"'" ;
        List<Map<String, Object>> accountlist = handlerService.findBySql(accountsql);
        Long  accountid = MapUtils.getLong( accountlist.get(0),"id");
        //合同变更已经做了添加，在这里不需要在进行重复添加
        if(!"conchangeApp".equals(appname)){
            List<TBFile> tbFiles = checkConNodeService.selectFiles(appname, id );
            //先删除，在添加
//        String del = "delete from tb_file where appname = 'conaccountApp' and recordid = '"+accountid+"'";
//        handlerService.updateBySql(del);
            if(CollectionUtils.isNotEmpty(tbFiles)){
                for (TBFile tbFile : tbFiles) {
                    //插入到合同台账
                    tbFile.setFileid(null);
                    tbFile.setAppname("conaccountApp");
                    tbFile.setRecordid(accountid);
                    checkConNodeService.insertConfiles(tbFile);
                }
            }
        }

        //过程资料
        List<TBFile> tbFiles2 = checkConNodeService.selectFiles("pro"+appname, id );
        if(CollectionUtils.isNotEmpty(tbFiles2)){
            for (TBFile tbFile : tbFiles2) {
                //插入到合同台账
                tbFile.setFileid(null);
                tbFile.setAppname("proaccountApp");
                tbFile.setRecordid(accountid);
                checkConNodeService.insertConfiles(tbFile);
            }
        }
        //财务资料
        List<TBFile> tbFiles3 = checkConNodeService.selectFiles("fin"+appname, id);
        if(CollectionUtils.isNotEmpty(tbFiles3)){
            for (TBFile tbFile : tbFiles3) {
                //插入到合同台账
                tbFile.setFileid(null);
                tbFile.setAppname("finaccountApp");
                tbFile.setRecordid(accountid);
                checkConNodeService.insertConfiles(tbFile);
            }
        }
        //交付物
        List<TBFile> tbFiles4 = checkConNodeService.selectFiles("deli"+appname, id );
        if(CollectionUtils.isNotEmpty(tbFiles4)){
            for (TBFile tbFile : tbFiles4) {
                //插入到合同台账
                tbFile.setFileid(null);
                tbFile.setAppname("deliaccountApp");
                tbFile.setRecordid(accountid);
                checkConNodeService.insertConfiles(tbFile);
            }
        }
    }

    @Override
    public void insertFiles(String appname, Long id, int conid,String filerealname) {
        List<TBFile> tbFiles = checkConNodeService.selectFiles(appname, id );
        String accountsql = "select id from gq_conaccount where  conid = '"+conid+"'" ;
        List<Map<String, Object>> accountlist = handlerService.findBySql(accountsql);
        Long  accountid = MapUtils.getLong( accountlist.get(0),"id");
        //先删除，在添加
        String del = "delete from tb_file where appname = 'conaccountApp' and recordid = '"+accountid+"' and filerealname= '"+filerealname+"'";
        handlerService.updateBySql(del);
        if(CollectionUtils.isNotEmpty(tbFiles)){
            for (TBFile tbFile : tbFiles) {
                //插入到合同台账
                tbFile.setFileid(null);
                tbFile.setAppname("conaccountApp");
                tbFile.setRecordid(accountid);
                filerealname = tbFile.getFilerealname();
                switch (appname){
                    case "conapplicationApp":filerealname="(合同申请)"+filerealname;break;
                    case "conreviewsApp":filerealname="(合同评审)"+filerealname;break;
                    case "consignsApp":filerealname="(合同签订)"+filerealname;break;
                }
                tbFile.setFilerealname(filerealname);
                checkConNodeService.insertConfiles(tbFile);
            }
        }
        //过程资料
        List<TBFile> tbFiles2 = checkConNodeService.selectFiles("pro"+appname, id );
        if(CollectionUtils.isNotEmpty(tbFiles2)){
            for (TBFile tbFile : tbFiles2) {
                //插入到合同台账
                tbFile.setFileid(null);
                tbFile.setAppname("proaccountApp");
                tbFile.setRecordid(accountid);
                checkConNodeService.insertConfiles(tbFile);
            }
        }
        //财务资料
        List<TBFile> tbFiles3 = checkConNodeService.selectFiles("fin"+appname, id);
        if(CollectionUtils.isNotEmpty(tbFiles3)){
            for (TBFile tbFile : tbFiles3) {
                //插入到合同台账
                tbFile.setFileid(null);
                tbFile.setAppname("finaccountApp");
                tbFile.setRecordid(accountid);
                checkConNodeService.insertConfiles(tbFile);
            }
        }
        //交付物
        List<TBFile> tbFiles4 = checkConNodeService.selectFiles("deli"+appname, id );
        if(CollectionUtils.isNotEmpty(tbFiles4)){
            for (TBFile tbFile : tbFiles4) {
                //插入到合同台账
                tbFile.setFileid(null);
                tbFile.setAppname("deliaccountApp");
                tbFile.setRecordid(accountid);
                checkConNodeService.insertConfiles(tbFile);
            }
        }
    }

    @Override
    public void processarc(String app, String bizId, String bizName, String concode, String conname) {

        String sql = "select * from  gq_processarc where concode = '"+concode+"' order by conid desc limit 1";
        List<Map<String, Object>> list = handlerService.findBySql(sql);
        Integer conid =1001;
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(list)) {
            conid = Integer.valueOf((String)list.get(0).get("conid"))+1;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("bizid",bizId);
        map.put("bizapp",app);
        map.put("bizname",bizName);
        map.put("concode",concode);
        map.put("conname",conname);
        map.put("conid",conid);
        try {
            handlerService.insert("gq_processarc",map);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateprocess(String concode) {
        //查询合同节点表，进行进度条的更新
        String sql = "select * from gq_connodenew where concode = '"+concode+"' and nodecode !=''";
        List<Map<String, Object>> list = handlerService.findBySql(sql);
        //总数
        int count = list.size();
        //完成数
        String sql2 = "select * from gq_connodenew where concode = '"+concode+"' and status = '已完成'";
        List<Map<String, Object>> list2 = handlerService.findBySql(sql2);
        //总数
        int count2 = list2.size();
        long process = count2*100/count;//Math.round(count2/count);
        String upd = "update gq_coninfo set process ="+process+" where concode = '"+concode+"' ";
        handlerService.updateBySql(upd);
        String upd2 = "update gq_conaccount set process ="+process+" where concode = '"+concode+"' ";
        handlerService.updateBySql(upd2);
    }


}
